package com.htjc.integral.account.config;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@EnableAsync
@Configuration
public class ThreadPool {

	private Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Value("${task.pool.corePoolSize}")
	public Integer corePoolSize;
	@Value("${task.pool.maxPoolSize}")
	public Integer maxPoolSize;
	@Value("${task.pool.queueCapacity}")
	public Integer queueCapacity;
	@Value("${task.pool.keepAliveSeconds}")
	public Integer keepAliveSeconds;
	
	
	@Bean
	public Executor taskExecutor(){
		log.info("***************Start TeskExecutor***************");
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setMaxPoolSize(maxPoolSize);
		executor.setCorePoolSize(corePoolSize);
		executor.setQueueCapacity(queueCapacity);
		executor.setKeepAliveSeconds(keepAliveSeconds);
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		executor.initialize();
		return executor;
	}
}
